﻿<UserControl x:Class="BalanceManagerApp.View.FlowFundsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:converters="clr-namespace:BalanceManagerApp.Helpers.Converters"
             mc:Ignorable="d" 
             d:DesignHeight="400" d:DesignWidth="700">
    <UserControl.Resources>
        <converters:FlowFundsGroupConverter x:Key="FlowFundsGroupConverter" />
        <converters:NewFlowFundsItemValueConverter x:Key="NewFlowFundsItemValueConverter" />
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <converters:BooleanToVisibilityConverterInvert IsReversed="True" x:Key="BooleanToVisibilityConverterInvert" />
        
        <CollectionViewSource Source="{Binding Items}" x:Key="cvs">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription Converter="{StaticResource FlowFundsGroupConverter}"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </UserControl.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="300" />
        </Grid.ColumnDefinitions>
            <ListBox Grid.Column="0" ItemsSource="{Binding Source={StaticResource cvs}}">
            <ListBox.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*" />
                                            </Grid.ColumnDefinitions>
                                            <Grid.RowDefinitions>
                                                <RowDefinition />
                                                <RowDefinition />
                                                <RowDefinition />
                                            </Grid.RowDefinitions>
                                            <TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="0" FontWeight="Bold" />
                                            <ItemsPresenter Grid.Row="1" Grid.Column="0" />
                                            <StackPanel x:Name="footerItemsPanel" Orientation="Horizontal" Grid.Row="2" Grid.Column="0">
                                                <StackPanel Name="newItemPanel"  Orientation="Horizontal" Visibility="{Binding IsChecked, ElementName=visibilityCheck, Converter={StaticResource BooleanToVisibilityConverterInvert}}">
                                                    <TextBox x:Name="txtNewItem" Text="Новая статья прихода/расхода" Width="160" />
                                                    <Button 
                                                    Width="16" Height="16" Margin="2,0,0,0"  
                                                    Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.NewItem.Command, Mode=OneWay}">
                                                        <Button.CommandParameter>
                                                            <MultiBinding Converter="{StaticResource NewFlowFundsItemValueConverter}">
                                                                <Binding Path="Text" ElementName="txtNewItem" />
                                                                <Binding Path="Name" />
                                                            </MultiBinding>
                                                        </Button.CommandParameter>
                                                        <Image Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.NewItem.Image, Mode=OneWay}" />
                                                    </Button>
                                                </StackPanel>
                                             
                                                <CheckBox IsChecked="True" x:Name="visibilityCheck" Visibility="{Binding IsChecked, ElementName=visibilityCheck, Converter={StaticResource BooleanToVisibilityConverter}, UpdateSourceTrigger=Default}">
                                                    <CheckBox.Template>
                                                        <ControlTemplate>
                                                            <StackPanel>
                                                                <TextBlock Foreground="Blue" Text="Добавить элемент" TextDecorations="Underline" Cursor="Hand"></TextBlock>
                                                            </StackPanel>
                                                        </ControlTemplate>
                                                    </CheckBox.Template>
                                                </CheckBox>
                                            </StackPanel>
                                        </Grid>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </ListBox.GroupStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="75" />
                            <ColumnDefinition Width="75" />
                            <ColumnDefinition Width="64" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Item.Name}" />
                        <StackPanel x:Name="stackPanel1" Orientation="Horizontal" Grid.Row="0" Grid.Column="1">
                            <TextBox Width="65" TextAlignment="Right" Text="{Binding IncomeAmount, UpdateSourceTrigger=PropertyChanged}" />
                        </StackPanel>
                        <StackPanel x:Name="stackPanel2" Orientation="Horizontal" Grid.Row="0" Grid.Column="2">
                            <TextBox Width="65" TextAlignment="Right" Text="{Binding ExpenseAmount, UpdateSourceTrigger=PropertyChanged}" />
                        </StackPanel>
                        <Button x:Name="btnMakeFlowFunds" Width="64" Height="16" Grid.Row="0" Grid.Column="3"
                                Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.MakeFlowFunds.Command, Mode=OneWay}"
                                CommandParameter="{Binding Path=.}">
                            <Image Height="16" Width="64" Source="../Resources/check.png" />
                        </Button>
                    </Grid>

                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ListBoxItem}}}" Value="True">
                            <Setter TargetName="stackPanel1" Property="Visibility" Value="Visible" />
                            <Setter TargetName="stackPanel2" Property="Visibility" Value="Visible" />
                            <Setter TargetName="btnMakeFlowFunds" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ListBoxItem}}}" Value="False">
                            <Setter TargetName="stackPanel1" Property="Visibility" Value="Hidden" />
                            <Setter TargetName="stackPanel2" Property="Visibility" Value="Hidden" />
                            <Setter TargetName="btnMakeFlowFunds" Property="Visibility" Value="Hidden" />
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
            <TabControl Grid.Column="1" >
                <TabItem Header="Сегодня">
                <ListView ItemsSource="{Binding TodayItemsList}">
                        <ListView.View>
                        <GridView>
                            <GridViewColumn Width="150" DisplayMemberBinding="{Binding Path=Item.Name}">Наименование</GridViewColumn>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=Date}">Дата</GridViewColumn>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=IncomeAmount}">Приход</GridViewColumn>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=ExpenseAmount}">Расход</GridViewColumn>
                        </GridView>
                        </ListView.View>
                    </ListView>
                </TabItem>
            <TabItem Header="Вчера">
                <ListView ItemsSource="{Binding YesterdayItemsList}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Width="150" DisplayMemberBinding="{Binding Path=Item.Name}">Наименование</GridViewColumn>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=Date}">Дата</GridViewColumn>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=IncomeAmount}">Приход</GridViewColumn>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=ExpenseAmount}">Расход</GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>
            </TabItem>
            <TabItem Header="Неделя">
                <ListView ItemsSource="{Binding LastWeekItemsList}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Width="150" DisplayMemberBinding="{Binding Path=Item.Name}">Наименование</GridViewColumn>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=Date}">Дата</GridViewColumn>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=IncomeAmount}">Приход</GridViewColumn>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=ExpenseAmount}">Расход</GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>
            </TabItem>
            <TabItem Header="Месяц">
                <ListView ItemsSource="{Binding LastMonthItemsList}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Width="150" DisplayMemberBinding="{Binding Path=Item.Name}">Наименование</GridViewColumn>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=Date}">Дата</GridViewColumn>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=IncomeAmount}">Приход</GridViewColumn>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=ExpenseAmount}">Расход</GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>
            </TabItem>
        </TabControl>
    </Grid>
</UserControl>
